package main

import (
	"fmt"
	"net"
	"time"
)

func main2() {
	// 连接本机1888端口
	conn, err := net.Dial("tcp", "127.0.0.1:1888")
	if err != nil {
		fmt.Printf("创建连接失败, 错误: %v\n", err)
		return
	}

	// 打印连接成功后的远端地址
	fmt.Printf("连接服务端成功：%v\n", conn.RemoteAddr())

	go func() {
		_, err = conn.Write([]byte("Hello"))

		if err != nil {
			fmt.Printf("发送消息失败, %v", err)
			return
		}

		time.Sleep(10 * time.Millisecond)

		var buf [1021]byte
		n, err := conn.Read(buf[:])
		if err != nil {
			fmt.Printf("read failed,err:%v\n", err)
			return
		}
		fmt.Println("收到服务端回复,", string(buf[:n]))
	}()

	time.Sleep(10 * time.Second)
}
